#ifndef _IO_H_
#define _IO_H_

#define RINGBUFFER_DEFAULT_SIZE 1024

// 环形缓冲区
typedef struct {
    char *buffer;       // 缓冲区指针
    int size;           // 缓冲区总容量
    int read_pos;       // 读位置
    int write_pos;      // 写位置
    int count;          // 当前数据量
} RingBuffer;

// epoll events.data.ptr定义
typedef struct
{
    int            fd;
    unsigned int   flag;         //fd flag
    unsigned int   events;
    FILE*          stream;
    void*          extend;
    char           name[1024];
}epmsg_t; 

//process function in readlines 
typedef void (*io_work)(void *,char *,FILE *);

typedef void (*fdstore_hook)(int,void*);


int pidfd_open(pid_t pid,unsigned int flags);
int pidfd_getfd(int pifd,int targetfd,unsigned int flags);
int move_fd(int oldfd,...);


epmsg_t *epmsg_init(int fd,int evts,FILE* stream,void *extend);
int epmsg_add(int epollfd,epmsg_t *epmsg);
int epmsg_del(int epollfd,epmsg_t *epmsg);
void freadlines(void* user,FILE *__in,FILE *__out,unsigned int iosize,io_work __worker);

RingBuffer *ringbuffer_create(int size);
void ringbuffer_destroy(RingBuffer *rb);
void ringbuffer_reset(RingBuffer *rb);
int ringbuffer_readable_bytes(RingBuffer *rb);
int ringbuffer_writable_bytes(RingBuffer *rb);
int ringbuffer_find_delimiter(RingBuffer *rb, char delim);
int ringbuffer_read(RingBuffer *rb, char *out, int len);
int ringbuffer_write(RingBuffer *rb, const char *data, int len);
int read_until_delimiter(int fd, RingBuffer *rb, char *output, int max_len, char delim);


#endif /* _IO_H_ */